package com.hoperun.product.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hoperun.product.Models.BoshaModel;
import com.hoperun.product.Models.UserModel;
import com.hoperun.product.Models.ZhenshaModel;
import com.hoperun.product.base.MIP_BaseActivity;
import com.hoperun.product.net.IServiceImpl;
import com.hoperun.product.net.RequestServiceImpl;
import com.hoperun.product.util.ParseUtils;
import com.hoperun.product.util.SharedPreUtil;
import com.hoperun.product.view.SimpleDialog;
import com.hoperun.yieldmgr.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BoListQueryActivity extends MIP_BaseActivity {

    @BindView(R.id.input_dateTv)
    TextView inputDateTv;
    @BindView(R.id.recylistview)
    RecyclerView recylistview;

    QuickAdapter quickAdapter;
    ArrayList<BoshaModel> daolist = new ArrayList<BoshaModel>();

    //日期
    String mJobDate;
    private SimpleDateFormat dateFormat_ZH = new SimpleDateFormat("yyyy年MM月dd日");
    private SimpleDateFormat dateFormat_EN = new SimpleDateFormat("yyyy-MM-dd");

    UserModel userModel=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bo_list_query);
        ButterKnife.bind(this);
        try {
            String jsonuser= SharedPreUtil.getValue(this,SharedPreUtil.USERBEAN,"").toString();
            userModel = ParseUtils.parse(jsonuser,UserModel.class);
            mJobDate = userModel.getJobDate();//dateFormat_EN.format(new Date(System.currentTimeMillis()));
            inputDateTv.setText(dateFormat_ZH.format(dateFormat_EN.parse(userModel.getJobDate())));
        }catch (Exception e){}


        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recylistview.setLayoutManager(layoutManager);
        //给RecyclerView设置适配器
        quickAdapter = new QuickAdapter();
        recylistview.setAdapter(quickAdapter);

        //默认查
        querylist(mJobDate,  userModel.getJobNum());
    }

    public void querylist(String JobDate, String gh) {//根据条件查询
        showWait();
        RequestServiceImpl.gettotalbolist(mMIPHandler, IServiceImpl.bosha_gettotal, JobDate,gh);
    }


    public class QuickAdapter extends BaseQuickAdapter<BoshaModel, BaseViewHolder> {
        public QuickAdapter() {
            super(R.layout.bo_total_list_item, daolist);
        }


        @Override
        protected void convert(final BaseViewHolder holder, BoshaModel item) {
            TextView item_date = holder.getView(R.id.item_date);//班次
            TextView item_banci = holder.getView(R.id.item_banci);//班次
            TextView item_bobanci = holder.getView(R.id.item_bobanci);//bo班次
            TextView item_gonghao = holder.getView(R.id.item_gonghao);//工号
            TextView item_maobai = holder.getView(R.id.item_maobai);
            TextView item_mahui = holder.getView(R.id.item_mahui);
            TextView item_yuanshi = holder.getView(R.id.item_yuanshi);
            TextView item_tiaozhen = holder.getView(R.id.item_tiaozhen);
            TextView item_dinge = holder.getView(R.id.item_dinge);


            item_date.setText(item.getJOBDATEString()==null?"--":item.getJOBDATEString());
            item_banci.setText(item.getPROCLASS()==null?"--":item.getPROCLASS());
            item_bobanci.setText(item.getSPCLASS()==null?"--":item.getSPCLASS());
            item_gonghao.setText(item.getJOBNUM()==null?"--":item.getJOBNUM());
            item_maobai.setText(item.getWHNUM()==null?"--":item.getWHNUM());
            item_mahui.setText(item.getHGREYNUM()==null?"--":item.getHGREYNUM());
            item_yuanshi.setText(item.getOLDPACKNUM()==null?"--":item.getOLDPACKNUM());
            item_tiaozhen.setText(item.getPACKNUM()==null?"--":item.getPACKNUM());
            item_dinge.setText(item.getQUOTA()==null?"--":item.getQUOTA());

        }
    }

    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
        String msg = "";
        String opt = "";
        if (error) {
            if (requestType != IServiceImpl.getYuanliaoInfo) {
                stopWait();
            }
            switch (requestType) {
                case IServiceImpl.bosha_gettotal://根据条件查询
                    try {
                        JSONObject jsonObj = new JSONObject((String) objBody);
                        if (jsonObj.getInt("Code") == 0) {
                            if (jsonObj.getJSONArray("Result") != null && jsonObj.getJSONArray("Result").length() > 0) {
                                JSONArray jsonArray = jsonObj.getJSONArray("Result");
                                daolist.clear();
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    BoshaModel daoshaModel = ParseUtils.parse(jsonArray.getJSONObject(i).toString(), BoshaModel.class);
                                    daolist.add(daoshaModel);
                                }
                            } else {
                                daolist.clear();
                            }
                            quickAdapter.notifyDataSetChanged();
                            checkListview();

                        }else{
                            daolist.clear();
                            quickAdapter.notifyDataSetChanged();
                            checkListview();
                            SimpleDialog.show(this, jsonObj.getString("Message"));
                        }
                    } catch (Exception e) {

                    }
                    break;
            }

        } else {
            stopWait();
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();

        }

    }

    /**
     * 检查listview是否为空
     */
    private void checkListview() {
        if (daolist.size() > 0) {
            recylistview.setVisibility(View.VISIBLE);
        } else {
            recylistview.setVisibility(View.GONE);
        }
    }


    @OnClick({R.id.input_dateLl, R.id.input_searchBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.input_dateLl:
                setDate();
                break;
            case R.id.input_searchBtn:
                querylist(mJobDate, userModel.getJobNum());

                break;
        }
    }

    /**
     * 设置日期
     */
    private void setDate() {
        // 初始化日历信息
        Calendar calendar = Calendar.getInstance();
        // 获取当前年月日
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        int minute=calendar.get(Calendar.MINUTE);
        if(hour<7||(hour==7&&minute<30)){
            day-=1;
        }
        Log.d("setDate", year + "-" + month + "-" + day);
        /**
         * context：当前上下文； callback：OnDateSetListener日期改变监听器； year：初始化的年；
         * monthOfYear：初始化的月（从0开始计数，所以实际应用时需要加1）； dayOfMonth：初始化的日；
         */
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, mOnDateSetListenernew, year, month, day);

        datePickerDialog.show();
    }

    /**
     * 日期选择监听器
     */
    DatePickerDialog.OnDateSetListener mOnDateSetListenernew = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            String date = year + "年" + String.format("%02d", month + 1) + "月" + String.format("%02d", dayOfMonth) + "日";
            inputDateTv.setText(date);
            mJobDate = year + "-" + String.format("%02d", month + 1) + "-" + String.format("%02d", dayOfMonth);

        }
    };


    private Calendar getDefaultDate(){
        Calendar calendar = Calendar.getInstance();
        // 获取当前年月日
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
//        int hour=calendar.get(Calendar.HOUR_OF_DAY);
//        int minute=calendar.get(Calendar.MINUTE);
//        if(hour<7||(hour==7&&minute<30)){
//            day-=1;
//        }
        calendar.set(year, month, day);
        return calendar;
    }

}
